﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BarCode.Common;
using System.IO;
using BarCode.Module;
using BarCode.AccessDao;
using System.Runtime.InteropServices;
using BarCode.Bean;
using System.IO.Ports;
using System.Threading;
using MySql.Data.MySqlClient;

namespace BarCode.Forms
{
    public partial class JK_BarCode : Form
    {
        [DllImport("user32.dll")]
        public static extern bool MessageBeep(uint uType);

        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(JK_BarCode));


        // 机壳查找检查
        private bool jkSearch = true;

        private SerialPort ComDevice = new SerialPort(); // 监视器串口
        private string stationNo = "";  // 工作区号
        private int workAmount = 0; // 工作量
        private int valveStopWait = 3;

        public JK_BarCode()
        {
            InitializeComponent();
        }

        public int getErrorInformation(int error_code, out string message)
        {
            int result = 0;
            byte[] errorInfo = new byte[1024];
            PrintLab.PTK_GetErrorInfo(error_code, errorInfo, errorInfo.Length);
            message = Encoding.Default.GetString(errorInfo).Trim('\0');
            return result;
        }


        private void showError(string FunctionName, int error_code)
        {
            PrintLab.PTK_CloseUSBPort();
            var message = "";
            int result = getErrorInformation(error_code, out message);
            MessageBox.Show(FunctionName + ":" + message);
        }

        private byte[] E_UTF8(string data)
        {
            return Encoding.UTF8.GetBytes(data);
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_Load(object sender, EventArgs e)
        {
            Console.WriteLine(GlobalVal.CurrentUser);
            lblUser.Text = GlobalVal.CurrentUser.User;
            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
            lblAmount.Text = workAmount + "";


            // 读取机壳查找设置
            /*string jkSearchVal = IniFileHelper.ReadValue("JXJK", "JK_Search_Enable");
            if (!"1".Equals(jkSearchVal) && !String.IsNullOrEmpty(jkSearchVal))
            {
                jkSearch = false;
            }
            
            lblUser.Text = GlobalVal.CurrentUser.User;
            lblStatus.Text = "当前为正常工作状态！";
            txtJKCode.Focus();
            if (GlobalVal.bolNetConnect)
            {
                btnNet.Enabled = false;
            }
            else
            {
                btnNet.Enabled = true;
            }

            // 工位号获取
            stationNo = IniFileHelper.getStringValue("System", "Work_Station");
            if (string.IsNullOrEmpty(stationNo))
            {
                MessageUtil.Error("工位号未设置，请设置后重启程序。");
                Application.Exit();
            }

            // 加载监视器
            bool loadRet = ComUtil.LoadMonitor(ComDevice);
            if (!loadRet)
            {
                MessageUtil.Error("监视器无法连接，请设置正确串口后重启程序。");
                Application.Exit();
            }

            // 停阀指令等待时间
            valveStopWait = IniFileHelper.getIntegerValue("JK", "VALVE_STOP_WAIT", 3);

            // 获取当前用户当天的工作量
            if (GlobalVal.bolNetConnect)
            {
                DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                int workAmount = db01.getJKWorkAmount(GlobalVal.CurrentUser.User);
                lblAmount.Text = (workAmount + GlobalVal.offSetInfo.jkoffset).ToString();
            }
            else
            {
                BarCodeMdbDao barcodeMdbDao = new BarCodeMdbDao();
                int workAmount = barcodeMdbDao.getJKWorkAmount(GlobalVal.CurrentUser.User);
                lblAmount.Text = (workAmount + GlobalVal.offSetInfo.jkoffset).ToString();
                barcodeMdbDao.Release();
            }

            GlobalVal.jkCountI_OK = Convert.ToInt32(lblAmount.Text);*/
            txtJKCode.Text = "";
            txtJKCode.Focus();
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (GlobalVal.bolNetConnect)
            {
                GlobalVal.bolUploadSubMain = false;
                modeFunction.upload_Data("jk");
            }
            Application.Exit();
        }


        /// <summary>
        /// 打印 按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNet_Click(object sender, EventArgs e)
        {
            string lineNo = GlobalVal.Line_No;
            string name = GlobalVal.CurrentUser.User;
            Console.WriteLine("添加" + GlobalVal.CurrentUser.User);
            string inputBarCode = txtJKCode.Text;
            if (GlobalVal.model.Equals("300s"))
            {
                if (lineNo.Equals("0022") || lineNo.Equals("0023"))
                {
                    if (GlobalVal.code2_one.Equals(""))
                    {
                        lblStatus.Text = "请扫描！";
                        lblStatus.ForeColor = System.Drawing.Color.Red;
                        return;
                    }
                    /*
        *mm:点，毫米转为点，由打印机分辨率决定
        * 打印机分辨DPI=203时， mm=8
        * 打印机分辨DPI=300时， mm=12
        * 打印机分辨DPI=600时， mm=24
        */
                    uint mm = 12;

                    int result = 0;
                    var message = "";

                    /*连接打印机*/
                    result = PrintLab.PTK_OpenUSBPort(255);
                    if (result != 0)
                    {
                        result = getErrorInformation(result, out message);
                        MessageBox.Show(message);
                        return;
                    }

                    /*函数返回值*/
                    int result_printing = 0;

                    /*横坐标偏移,设置标签整体横向移动位置*/
                    int offset_X = 60;

                    /*纵坐标偏移，设置标签整体纵向移动位置*/
                    int offset_Y = 0;

                    /*清空缓存*/
                    result_printing = PrintLab.PTK_ClearBuffer();
                    if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }

                    /*设置打印黑度 取值范围 0-20*/
                    result_printing = PrintLab.PTK_SetDarkness(10);
                    if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }

                    /*设置打印速度*/
                    result_printing = PrintLab.PTK_SetPrintSpeed(4);
                    if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }

                    /*设置打印方向*/
                    result_printing = PrintLab.PTK_SetDirection('B');
                    if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }

                    /*设置标签高度、间隙及偏移*/
                    /*result_printing = PrintLab.PTK_SetLabelHeight(12 * mm, 0, 2 * mm, false);
                    if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }*/

                    /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
                    result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
                    if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }

                    /*打印二维码 此示例为QR码 付款码就是这个码*/
                    result_printing = PrintLab.PTK_DrawBar2D_QR((uint)(10 * mm), (uint)(5 * mm), 0, 1, 0, 4, 4, 3, 8, E_UTF8(GlobalVal.code2_one));
                    if (result_printing != 0) { showError("PTK_DrawBar2D_QR", result_printing); return; }

                    /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
                    result_printing = PrintLab.PTK_PrintLabel(1, 1);
                    if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }

                    /*断开打印机连接*/
                    PrintLab.PTK_CloseUSBPort();
                    txtJKCode.Text = "";
                    lblStatus.Text = "打印成功！";
                    lblStatus.ForeColor = System.Drawing.Color.Blue;
                    GlobalVal.code2_one = "";
                }
                else
                {
                    if (GlobalVal.code_one.Equals(""))
                    {
                        lblStatus.Text = "请扫描！";
                        lblStatus.ForeColor = System.Drawing.Color.Red;
                        return;
                    }
                    if (GlobalVal.code_one.Length != 19)
                    {
                        ShowInputWarn("条码信息位数不正确！", Consts.MB_ICONASTERISK);
                        lblStatus.Text = "条码信息位数不正确！";
                        lblStatus.ForeColor = System.Drawing.Color.Red;
                        return;
                    }
                    /*
                  *mm:点，毫米转为点，由打印机分辨率决定
                  * 打印机分辨DPI=203时， mm=8
                  * 打印机分辨DPI=300时， mm=12
                  * 打印机分辨DPI=600时， mm=24
                */
                    uint mm = 12;

                    int result = 0;
                    var message = "";

                    /*连接打印机*/
                    result = PrintLab.PTK_OpenUSBPort(255);
                    if (result != 0)
                    {
                        result = getErrorInformation(result, out message);
                        MessageBox.Show(message);
                        return;
                    }

                    /*函数返回值*/
                    int result_printing = 0;

                    /*横坐标偏移,设置标签整体横向移动位置*/
                    int offset_X = 60;

                    /*纵坐标偏移，设置标签整体纵向移动位置*/
                    int offset_Y = 0;

                    /*清空缓存*/
                    result_printing = PrintLab.PTK_ClearBuffer();
                    if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }

                    /*设置打印黑度 取值范围 0-20*/
                    result_printing = PrintLab.PTK_SetDarkness(10);
                    if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }

                    /*设置打印速度*/
                    result_printing = PrintLab.PTK_SetPrintSpeed(4);
                    if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }

                    /*设置打印方向*/
                    result_printing = PrintLab.PTK_SetDirection('B');
                    if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }

                    /*设置标签高度、间隙及偏移*/
                    /*result_printing = PrintLab.PTK_SetLabelHeight(11 * mm, 0, 2 * mm, false);
                    if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }*/

                    /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
                    result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
                    if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }

                    /*打印一维码 此示例为Code128码*/
                    result_printing = PrintLab.PTK_DrawBarcode((uint)(0.1 * mm), (uint)(2.3 * mm + offset_Y), 0, "1", 2, 6, 4 * mm, 'N', E_UTF8(GlobalVal.code_one));//打印一维码
                    if (result_printing != 0) { showError("PTK_DrawBarcode", result_printing); return; }

                    /* 打印条码下方文字*/
                    result_printing = PrintLab.PTK_DrawText_TrueType((uint)(13 * mm), (uint)(7.6 * mm), 2 * mm, 0, E_UTF8("Arial"), 5, 700, false, false, false, E_UTF8(GlobalVal.code_one));
                    if (result_printing != 0) { showError("PTK_DrawText_TrueType", result_printing); return; }


                    /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
                    result_printing = PrintLab.PTK_PrintLabel(1, 1);
                    if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }

                    /*断开打印机连接*/
                    PrintLab.PTK_CloseUSBPort();
                    txtJKCode.Text = "";
                    lblStatus.Text = "打印成功！";
                    lblStatus.ForeColor = System.Drawing.Color.Blue;
                    GlobalVal.code_one = "";
                }
            }
            else
            {
                if (lineNo.Equals("0022") || lineNo.Equals("0023"))
                {
                    if (GlobalVal.code2_one.Equals(""))
                    {
                        lblStatus.Text = "请扫描！";
                        lblStatus.ForeColor = System.Drawing.Color.Red;
                        return;
                    }
                    /*
        *mm:点，毫米转为点，由打印机分辨率决定
        * 打印机分辨DPI=203时， mm=8
        * 打印机分辨DPI=300时， mm=12
        * 打印机分辨DPI=600时， mm=24
        */
                    uint mm = 12;

                    int result = 0;
                    var message = "";

                    /*连接打印机*/
                    result = PrintLab.PTK_OpenUSBPort(255);
                    if (result != 0)
                    {
                        result = getErrorInformation(result, out message);
                        MessageBox.Show(message);
                        return;
                    }

                    /*函数返回值*/
                    int result_printing = 0;

                    /*横坐标偏移,设置标签整体横向移动位置*/
                    int offset_X = 60;

                    /*纵坐标偏移，设置标签整体纵向移动位置*/
                    int offset_Y = 0;

                    /*清空缓存*/
                    result_printing = PrintLab.PTK_ClearBuffer();
                    if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }

                    /*设置打印黑度 取值范围 0-20*/
                    result_printing = PrintLab.PTK_SetDarkness(10);
                    if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }

                    /*设置打印速度*/
                    result_printing = PrintLab.PTK_SetPrintSpeed(4);
                    if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }

                    /*设置打印方向*/
                    result_printing = PrintLab.PTK_SetDirection('B');
                    if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }

                    /*设置标签高度、间隙及偏移*/
                    /*result_printing = PrintLab.PTK_SetLabelHeight(12 * mm, 0, 2 * mm, false);
                    if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }*/

                    /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
                    result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
                    if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }

                    /*打印二维码 此示例为QR码 付款码就是这个码*/
                    result_printing = PrintLab.PTK_DrawBar2D_QR((uint)(16 * mm), (uint)(3 * mm), 0, 1, 0, 3, 4, 3, 8, E_UTF8(GlobalVal.code2_one));
                    if (result_printing != 0) { showError("PTK_DrawBar2D_QR", result_printing); return; }

                    /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
                    result_printing = PrintLab.PTK_PrintLabel(1, 1);
                    if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }

                    /*断开打印机连接*/
                    PrintLab.PTK_CloseUSBPort();
                    txtJKCode.Text = "";
                    lblStatus.Text = "打印成功！";
                    lblStatus.ForeColor = System.Drawing.Color.Blue;
                    GlobalVal.code2_one = "";
                }
                else
                {
                    if (GlobalVal.code_one.Equals(""))
                    {
                        lblStatus.Text = "请扫描！";
                        lblStatus.ForeColor = System.Drawing.Color.Red;
                        return;
                    }
                    if (GlobalVal.code_one.Length != 19)
                    {
                        ShowInputWarn("条码信息位数不正确！", Consts.MB_ICONASTERISK);
                        lblStatus.Text = "条码信息位数不正确！";
                        lblStatus.ForeColor = System.Drawing.Color.Red;
                        return;
                    }
                    /*
                  *mm:点，毫米转为点，由打印机分辨率决定
                  * 打印机分辨DPI=203时， mm=8
                  * 打印机分辨DPI=300时， mm=12
                  * 打印机分辨DPI=600时， mm=24
                */
                    uint mm = 12;

                    int result = 0;
                    var message = "";

                    /*连接打印机*/
                    result = PrintLab.PTK_OpenUSBPort(255);
                    if (result != 0)
                    {
                        result = getErrorInformation(result, out message);
                        MessageBox.Show(message);
                        return;
                    }

                    /*函数返回值*/
                    int result_printing = 0;

                    /*横坐标偏移,设置标签整体横向移动位置*/
                    int offset_X = 60;

                    /*纵坐标偏移，设置标签整体纵向移动位置*/
                    int offset_Y = 0;

                    /*清空缓存*/
                    result_printing = PrintLab.PTK_ClearBuffer();
                    if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }

                    /*设置打印黑度 取值范围 0-20*/
                    result_printing = PrintLab.PTK_SetDarkness(10);
                    if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }

                    /*设置打印速度*/
                    result_printing = PrintLab.PTK_SetPrintSpeed(4);
                    if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }

                    /*设置打印方向*/
                    result_printing = PrintLab.PTK_SetDirection('B');
                    if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }

                    /*设置标签高度、间隙及偏移*/
                    /*result_printing = PrintLab.PTK_SetLabelHeight(11 * mm, 0, 2 * mm, false);
                    if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }*/

                    /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
                    result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
                    if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }

                    /*打印一维码 此示例为Code128码*/
                    result_printing = PrintLab.PTK_DrawBarcode((uint)(11.7 * mm), (uint)(2.3 * mm + offset_Y), 0, "1", (uint)1 , 6, 3 * mm, 'N', E_UTF8(GlobalVal.code_one));//打印一维码
                    if (result_printing != 0) { showError("PTK_DrawBarcode", result_printing); return; }

                    /* 打印条码下方文字*/
                    result_printing = PrintLab.PTK_DrawText_TrueType((uint)(18.7 * mm), (uint)(6 * mm), 13, 0, E_UTF8("Arial"), 5, 600, false, false, false, E_UTF8(GlobalVal.code_one));
                    if (result_printing != 0) { showError("PTK_DrawText_TrueType", result_printing); return; }


                    /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
                    result_printing = PrintLab.PTK_PrintLabel(1, 1);
                    if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }

                    /*断开打印机连接*/
                    PrintLab.PTK_CloseUSBPort();
                    txtJKCode.Text = "";
                    lblStatus.Text = "打印成功！";
                    lblStatus.ForeColor = System.Drawing.Color.Blue;
                    GlobalVal.code_one = "";
                }
            }

            /*DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            if (db01.EnableConnect() && Consts.CONNECT_SUCCESS.Equals(db01.getConnetText()))
            {
                GlobalVal.bolNetConnect = true;
                btnNet.Enabled = false;
            }
            else
            {
                GlobalVal.bolNetConnect = false;
                btnNet.Enabled = true;
            }*/
        }

        /// <summary>
        /// 二维码输入后回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            //打印机型号300S
            if (GlobalVal.model.Equals("300s"))
            {
                lblAmount.BackColor = Color.Transparent;
                if (e.KeyChar == (char)13)
                {
                    string lineNo = GlobalVal.Line_No;
                    string name = GlobalVal.CurrentUser.User;
                    Console.WriteLine("添加" + GlobalVal.CurrentUser.User);
                    string inputBarCode = txtJKCode.Text;

                    //// 条码长度检查
                    //if (inputBarCode.Length < 11 || inputBarCode.Length > 12)
                    //{
                    //    ShowInputWarn("条码信息位数不足！",Consts.MB_ICONASTERISK);
                    //    return;
                    //}
                    // 条码长度检查  整表 条形码  注塑/机芯 二维码   
                    if (lineNo.Equals("0022") || lineNo.Equals("0023"))
                    {
                        // 条码长度检查  整表 条形码  注塑/机芯 二维码   
                        if (inputBarCode.Length != 13)
                        {
                            ShowInputWarn("条码信息位数不正确！", Consts.MB_ICONASTERISK);
                            lblStatus.ForeColor = System.Drawing.Color.Red;
                            lblStatus.Text = "条码信息位数不正确！";
                            return;
                        }
                        if (GlobalVal.code2_one.Equals(""))
                        {
                            GlobalVal.code2_one = inputBarCode;
                            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                            int i = db01.getBmCode(inputBarCode);
                            if (i != 0)
                            {
                                db01.editBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            else
                            {
                                db01.inertBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = workAmount + "";
                            txtJKCode.Text = "";
                        }
                        else if (GlobalVal.code2_one.Equals(inputBarCode))
                        {
                            lblStatus.Text = "重复扫描！";
                            lblStatus.ForeColor = System.Drawing.Color.Red;
                            txtJKCode.Text = "";
                            return;
                        }
                        else if (!GlobalVal.code2_one.Equals("") && GlobalVal.code2_two.Equals(""))
                        {
                            GlobalVal.code2_two = inputBarCode;
                            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                            int i = db01.getBmCode(inputBarCode);
                            if (i != 0)
                            {
                                db01.editBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            else
                            {
                                db01.inertBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            /*
                *mm:点，毫米转为点，由打印机分辨率决定
                * 打印机分辨DPI=203时， mm=8
                * 打印机分辨DPI=300时， mm=12
                * 打印机分辨DPI=600时， mm=24
                */
                            uint mm = 12;

                            int result = 0;
                            var message = "";

                            /*连接打印机*/
                            result = PrintLab.PTK_OpenUSBPort(255);
                            if (result != 0)
                            {
                                result = getErrorInformation(result, out message);
                                MessageBox.Show(message);
                                return;
                            }

                            /*函数返回值*/
                            int result_printing = 0;

                            /*横坐标偏移,设置标签整体横向移动位置*/
                            int offset_X = 60;

                            /*纵坐标偏移，设置标签整体纵向移动位置*/
                            int offset_Y = 0;

                            /*清空缓存*/
                            result_printing = PrintLab.PTK_ClearBuffer();
                            if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }

                            /*设置打印黑度 取值范围 0-20*/
                            result_printing = PrintLab.PTK_SetDarkness(10);
                            if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }

                            /*设置打印速度*/
                            result_printing = PrintLab.PTK_SetPrintSpeed(4);
                            if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }

                            /*设置打印方向*/
                            result_printing = PrintLab.PTK_SetDirection('B');
                            if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }

                            /*设置标签高度、间隙及偏移*/
                            /*result_printing = PrintLab.PTK_SetLabelHeight(12 * mm, 0, 2 * mm, false);
                            if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }*/

                            /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
                            result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
                            if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }

                            /*打印二维码 此示例为QR码 付款码就是这个码*/
                            result_printing = PrintLab.PTK_DrawBar2D_QR((uint)(10 * mm), (uint)(5 * mm), 0, 1, 0, 4, 4, 3, 8, E_UTF8(GlobalVal.code2_one));
                            if (result_printing != 0) { showError("PTK_DrawBar2D_QR", result_printing); return; }

                            /*打印二维码 此示例为QR码 付款码就是这个码*/
                            result_printing = PrintLab.PTK_DrawBar2D_QR((uint)(40.8 * mm), (uint)(5 * mm), 0, 1, 0, 4, 4, 3, 8, E_UTF8(GlobalVal.code2_two));
                            if (result_printing != 0) { showError("PTK_DrawBar2D_QR", result_printing); return; }

                            /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
                            result_printing = PrintLab.PTK_PrintLabel(1, 1);
                            if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }

                            /*断开打印机连接*/
                            PrintLab.PTK_CloseUSBPort();
                            GlobalVal.code2_one = "";
                            GlobalVal.code2_two = "";
                            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = workAmount + "";
                            txtJKCode.Text = "";
                        }
                        lblStatus.Text = "扫描成功！";
                        lblStatus.ForeColor = System.Drawing.Color.Blue;
                    }
                    else
                    {
                        if (inputBarCode.Length != 19)
                        {
                            ShowInputWarn("条码信息位数不正确！", Consts.MB_ICONASTERISK);
                            lblStatus.Text = "条码信息位数不正确！";
                            lblStatus.ForeColor = System.Drawing.Color.Red;
                            return;
                        }
                        if (GlobalVal.code_one.Equals(""))
                        {
                            GlobalVal.code_one = inputBarCode;
                            string name1 = GlobalVal.workShop_name;
                            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                            int i = db01.getBmCode(inputBarCode);
                            if (i != 0)
                            {
                                db01.editBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            else
                            {
                                db01.inertBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = workAmount + "";
                            txtJKCode.Text = "";
                        }
                        else if (GlobalVal.code_one.Equals(inputBarCode))
                        {
                            lblStatus.Text = "重复扫描！";
                            lblStatus.ForeColor = System.Drawing.Color.Red;
                            txtJKCode.Text = "";
                            return;
                        }
                        else if (!GlobalVal.code_one.Equals("") && GlobalVal.code_two.Equals(""))
                        {
                            GlobalVal.code_two = inputBarCode;
                            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                            int i = db01.getBmCode(inputBarCode);
                            if (i != 0)
                            {
                                db01.editBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            else
                            {
                                db01.inertBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            /*
                          *mm:点，毫米转为点，由打印机分辨率决定
                          * 打印机分辨DPI=203时， mm=8
                          * 打印机分辨DPI=300时， mm=12
                          * 打印机分辨DPI=600时， mm=24
                        */
                            uint mm = 12;

                            int result = 0;
                            var message = "";
                            /*测试git*/
                            /*连接打印机*/
                            result = PrintLab.PTK_OpenUSBPort(255);
                            if (result != 0)
                            {
                                result = getErrorInformation(result, out message);
                                MessageBox.Show(message);
                                return;
                            }

                            /*函数返回值*/
                            int result_printing = 0;

                            /*横坐标偏移,设置标签整体横向移动位置*/
                            int offset_X = 60;

                            /*纵坐标偏移，设置标签整体纵向移动位置*/
                            int offset_Y = 0;

                            /*清空缓存*/
                            result_printing = PrintLab.PTK_ClearBuffer();
                            if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }

                            /*设置打印黑度 取值范围 0-20*/
                            result_printing = PrintLab.PTK_SetDarkness(10);
                            if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }

                            /*设置打印速度*/
                            result_printing = PrintLab.PTK_SetPrintSpeed(4);
                            if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }

                            /*设置打印方向*/
                            result_printing = PrintLab.PTK_SetDirection('B');
                            if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }

                            /*设置标签高度、间隙及偏移*/
                            /*result_printing = PrintLab.PTK_SetLabelHeight(11 * mm, 0, 2 * mm, false);
                            if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }*/

                            /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
                            result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
                            if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }

                            /*打印一维码 此示例为Code128码*/
                            result_printing = PrintLab.PTK_DrawBarcode((uint)(0.1 * mm), (uint)(2.3 * mm + offset_Y), 0, "1", 2, 6, 4 * mm, 'N', E_UTF8(GlobalVal.code_one));//打印一维码
                            if (result_printing != 0) { showError("PTK_DrawBarcode", result_printing); return; }

                            /* 打印条码下方文字*/
                            result_printing = PrintLab.PTK_DrawText_TrueType((uint)(13 * mm), (uint)(7.6 * mm), 2 * mm, 0, E_UTF8("Arial"), 5, 700, false, false, false, E_UTF8(GlobalVal.code_one));
                            if (result_printing != 0) { showError("PTK_DrawText_TrueType", result_printing); return; }

                            result_printing = PrintLab.PTK_DrawBarcode((uint)(33.5 * mm), (uint)(2.3 * mm + offset_Y), 0, "1", 2, 6, 4 * mm, 'N', E_UTF8(GlobalVal.code_two));//打印一维码
                            if (result_printing != 0) { showError("PTK_DrawBarcode", result_printing); return; }

                            /* 打印条码下方文字*/
                            result_printing = PrintLab.PTK_DrawText_TrueType((uint)(46.5 * mm), (uint)(7.6 * mm), 2 * mm, 0, E_UTF8("Arial"), 5, 700, false, false, false, E_UTF8(GlobalVal.code_two));
                            if (result_printing != 0) { showError("PTK_DrawText_TrueType", result_printing); return; }


                            /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
                            result_printing = PrintLab.PTK_PrintLabel(1, 1);
                            if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }

                            /*断开打印机连接*/
                            PrintLab.PTK_CloseUSBPort();
                            GlobalVal.code_one = "";
                            GlobalVal.code_two = "";
                            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = workAmount + "";
                            txtJKCode.Text = "";
                        }
                        lblStatus.Text = "扫描成功！";
                        lblStatus.ForeColor = System.Drawing.Color.Blue;

                        //;

                    }
                }
            }
            //打印机型号200s
            else
            {
                lblAmount.BackColor = Color.Transparent;
                if (e.KeyChar == (char)13)
                {
                    string lineNo = GlobalVal.Line_No;
                    string name = GlobalVal.CurrentUser.User;
                    string inputBarCode = txtJKCode.Text;

                    // 条码长度检查  整表 条形码  注塑/机芯 二维码   
                    if (lineNo.Equals("0022") || lineNo.Equals("0023"))
                    {
                        // 条码长度检查  整表 条形码  注塑/机芯 二维码   
                        if (inputBarCode.Length != 13)
                        {
                            ShowInputWarn("条码信息位数不正确！", Consts.MB_ICONASTERISK);
                            lblStatus.ForeColor = System.Drawing.Color.Red;
                            lblStatus.Text = "条码信息位数不正确！";
                            return;
                        }
                        // 二维码一 为空
                        if (GlobalVal.code2_one.Equals(""))
                        {
                            GlobalVal.code2_one = inputBarCode;
                            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                            int i = db01.getBmCode(inputBarCode);
                            if (i != 0)
                            {
                                db01.editBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            else
                            {
                                db01.inertBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = workAmount + "";
                            txtJKCode.Text = "";
                        }
                        else if (GlobalVal.code2_one.Equals(inputBarCode))
                        {
                            lblStatus.Text = "重复扫描！";
                            lblStatus.ForeColor = System.Drawing.Color.Red;
                            txtJKCode.Text = "";
                            return;
                        }
                        //二维码一不为空 二维码二为空
                        else if (!GlobalVal.code2_one.Equals("") && GlobalVal.code2_two.Equals(""))
                        {
                            GlobalVal.code2_two = inputBarCode;
                            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                            int i = db01.getBmCode(inputBarCode);
                            if (i != 0)
                            {
                                db01.editBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            else
                            {
                                db01.inertBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            /*
                *mm:点，毫米转为点，由打印机分辨率决定
                * 打印机分辨DPI=203时， mm=8
                * 打印机分辨DPI=300时， mm=12
                * 打印机分辨DPI=600时， mm=24
                */
                            uint mm = 12;

                            int result = 0;
                            var message = "";

                            /*连接打印机*/
                            result = PrintLab.PTK_OpenUSBPort(255);
                            if (result != 0)
                            {
                                result = getErrorInformation(result, out message);
                                MessageBox.Show(message);
                                return;
                            }

                            /*函数返回值*/
                            int result_printing = 0;

                            /*横坐标偏移,设置标签整体横向移动位置*/
                            int offset_X = 60;

                            /*纵坐标偏移，设置标签整体纵向移动位置*/
                            int offset_Y = 0;

                            /*清空缓存*/
                            result_printing = PrintLab.PTK_ClearBuffer();
                            if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }

                            /*设置打印黑度 取值范围 0-20*/
                            result_printing = PrintLab.PTK_SetDarkness(10);
                            if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }

                            /*设置打印速度*/
                            result_printing = PrintLab.PTK_SetPrintSpeed(4);
                            if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }

                            /*设置打印方向*/
                            result_printing = PrintLab.PTK_SetDirection('B');
                            if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }

                            /*设置标签高度、间隙及偏移*/
                            /*result_printing = PrintLab.PTK_SetLabelHeight(12 * mm, 0, 2 * mm, false);
                            if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }*/

                            /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
                            result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
                            if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }

                            /*打印二维码 二维码一*/
                            result_printing = PrintLab.PTK_DrawBar2D_QR((uint)(16 * mm), (uint)(3*mm), 0, 1, 0, 3, 4, 3, 8, E_UTF8(GlobalVal.code2_one));
                            if (result_printing != 0) { showError("PTK_DrawBar2D_QR", result_printing); return; }

                            /*打印二维码 二维码二*/
                            result_printing = PrintLab.PTK_DrawBar2D_QR((uint)(37.8 * mm), (uint)(3 * mm), 0, 1, 0, 3, 4, 3, 8, E_UTF8(GlobalVal.code2_two));
                            if (result_printing != 0) { showError("PTK_DrawBar2D_QR", result_printing); return; }

                            /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
                            result_printing = PrintLab.PTK_PrintLabel(1, 1);
                            if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }

                            /*断开打印机连接*/
                            PrintLab.PTK_CloseUSBPort();
                            GlobalVal.code2_one = "";
                            GlobalVal.code2_two = "";
                            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = workAmount + "";
                            txtJKCode.Text = "";
                        }
                        lblStatus.Text = "扫描成功！";
                        lblStatus.ForeColor = System.Drawing.Color.Blue;
                    }
                    //条码打印
                    else
                    {
                        if (inputBarCode.Length != 19)
                        {
                            ShowInputWarn("条码信息位数不正确！", Consts.MB_ICONASTERISK);
                            lblStatus.Text = "条码信息位数不正确！";
                            lblStatus.ForeColor = System.Drawing.Color.Red;
                            return;
                        }
                        if (GlobalVal.code_one.Equals(""))
                        {
                            GlobalVal.code_one = inputBarCode;
                            string name1 = GlobalVal.workShop_name;
                            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                            int i = db01.getBmCode(inputBarCode);
                            if(i != 0)
                            {
                                db01.editBM(inputBarCode, GlobalVal.CurrentUser.User, name1);
                            }
                            else
                            {
                                db01.inertBM(inputBarCode, GlobalVal.CurrentUser.User, name1);
                            }
                            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = workAmount + "";
                            txtJKCode.Text = "";
                        }else if (GlobalVal.code_one.Equals(inputBarCode))
                        {
                            lblStatus.Text = "重复扫描！";
                            lblStatus.ForeColor = System.Drawing.Color.Red;
                            txtJKCode.Text = "";
                            return;
                        }
                        else if (!GlobalVal.code_one.Equals("") && GlobalVal.code_two.Equals(""))
                        {
                            GlobalVal.code_two = inputBarCode;
                            DB01Dao db01 = new DB01Dao(GlobalVal.ipAddressInfo.SERVICE_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                            int i = db01.getBmCode(inputBarCode);
                            if (i != 0)
                            {
                                db01.editBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            else
                            {
                                db01.inertBM(inputBarCode, GlobalVal.CurrentUser.User, GlobalVal.workShop_name);
                            }
                            
                            /*
                          *mm:点，毫米转为点，由打印机分辨率决定
                          * 打印机分辨DPI=203时， mm=8
                          * 打印机分辨DPI=300时， mm=12
                          * 打印机分辨DPI=600时， mm=24
                        */
                            uint mm = 12;

                            int result = 0;
                            var message = "";
                            /*测试git*/
                            /*连接打印机*/
                            result = PrintLab.PTK_OpenUSBPort(255);
                            if (result != 0)
                            {
                                result = getErrorInformation(result, out message);
                                MessageBox.Show(message);
                                return;
                            }

                            /*函数返回值*/
                            int result_printing = 0;

                            /*横坐标偏移,设置标签整体横向移动位置*/
                            int offset_X = 60;

                            /*纵坐标偏移，设置标签整体纵向移动位置*/
                            int offset_Y = 0;

                            /*清空缓存*/
                            result_printing = PrintLab.PTK_ClearBuffer();
                            if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }

                            /*设置打印黑度 取值范围 0-20*/
                            result_printing = PrintLab.PTK_SetDarkness(10);
                            if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }

                            /*设置打印速度*/
                            result_printing = PrintLab.PTK_SetPrintSpeed(4);
                            if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }

                            /*设置打印方向*/
                            result_printing = PrintLab.PTK_SetDirection('B');
                            if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }

                            /*设置标签高度、间隙及偏移*/
                            /*result_printing = PrintLab.PTK_SetLabelHeight(11 * mm, 0, 2 * mm, false);
                            if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }*/

                            /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
                            result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
                            if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }

                            /*打印一维码 此示例为Code128码*/
                            result_printing = PrintLab.PTK_DrawBarcode((uint)(11.7 * mm), (uint)(2.3 * mm + offset_Y), 0, "1", (uint)1, 6, 3 * mm, 'N', E_UTF8(GlobalVal.code_one));//打印一维码
                            if (result_printing != 0) { showError("PTK_DrawBarcode", result_printing); return; }

                            /* 打印条码下方文字*/
                            result_printing = PrintLab.PTK_DrawText_TrueType((uint)(18.7 * mm), (uint)(6 * mm), 13, 0, E_UTF8("Arial"), 5, 600, false, false, false, E_UTF8(GlobalVal.code_one));
                            if (result_printing != 0) { showError("PTK_DrawText_TrueType", result_printing); return; }
                            // 条形码二
                            result_printing = PrintLab.PTK_DrawBarcode((uint)(34.5 * mm), (uint)(2.3 * mm + offset_Y), 0, "1", 1, 6, 3 * mm, 'N', E_UTF8(GlobalVal.code_two));//打印一维码
                            if (result_printing != 0) { showError("PTK_DrawBarcode", result_printing); return; }

                            /* 打印条码下方文字*/
                            result_printing = PrintLab.PTK_DrawText_TrueType((uint)(41.5 * mm), (uint)(6 * mm), 13, 0, E_UTF8("Arial"), 5, 600, false, false, false, E_UTF8(GlobalVal.code_two));
                            if (result_printing != 0) { showError("PTK_DrawText_TrueType", result_printing); return; }


                            /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
                            result_printing = PrintLab.PTK_PrintLabel(1, 1);
                            if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }

                            /*断开打印机连接*/
                            PrintLab.PTK_CloseUSBPort();
                            GlobalVal.code_one = "";
                            GlobalVal.code_two = "";
                            int workAmount = db01.getWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = workAmount + "";
                            txtJKCode.Text = "";
                        }
                        lblStatus.Text = "扫描成功！";
                        lblStatus.ForeColor = System.Drawing.Color.Blue;

                        //;

                    }
                }
            }

        }

        /// <summary>
        /// 输入有误时弹框显示
        /// </summary>
        /// <param name="msg"></param>
        private void ShowInputWarn(string msg, uint beep)
        {
            txtJKCode.Text = "";
            txtJKCode.Focus();
            //txtJXCode.Text = "";
            //lblStatus.Text = msg;
            if (beep > 0)
            {
                MessageBeep(beep);
            }
        }

        private void txtJKCode_TextChanged(object sender, EventArgs e)
        {

        }

        private void JK_BarCode_Load(object sender, EventArgs e)
        {

        }
    }
}
